package android.adapters;

import java.util.List;

import android.preference.PreferenceManager.OnActivityResultListener;
import android.sax.StartElementListener;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Context;
import android.content.Intent;

import android.model.BankItem;
import android.activity.R;
import android.activity.TextToSpeach;
import android.activity.R.string;
import android.app.Activity;


public class BankAdapter extends BaseAdapter {
	
	//kreiranje na lista od stavki za bankata
	List<BankItem> items;
	LayoutInflater inflater;
	
	//TextToSpeach ttl1;
	OnClickListener l;
	
	//se definira vo kakvi komponenti ke se prikazuvaat podatocite
	static class BankItemHolder {
		
		TextView wordView;
		TextView descriptionView;
		
	}
	
	Activity myMainActivity;

	    public BankAdapter(Activity mainActivity) {
		// TODO Auto-generated constructor stub
		   super();
	       this.myMainActivity=mainActivity;
	}
	           
	   
	
	//kreiranje na konstruktor vo koj se dodava kakov e layout-ot na ovaa klasa
	public BankAdapter(Context ctx, List<BankItem> items) {
		this.items = items;
		inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	/*
	 * (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 * se vraka goleminata na stavkite
	 */
	public int getCount() {
		return items.size();
	}

	/*
	 * (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 * za sekoja stavka se vraka pozicijata
	 */
	public Object getItem(int position) {
		return items.get(position);
	}

	/*
	 * (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 * se vraka id-to na stavkata
	 */
	public long getItemId(int position) {
		return position;
	}

	/*
	 * se vrsi kreiranje na View za toa na koj nacin ke se prikazat podatocite
	 * se kreira layout-ot vo koj se prikazuvaat podatocite
	 * kako i koja komponenta koj podatok ke go prikazuva
	 */
	public View getView(final int position, View convertView, ViewGroup parent) {
		final BankItemHolder bih;
		if (convertView == null) {
			RelativeLayout rl = (RelativeLayout) inflater.inflate(R.layout.v_bank_item, null);
			convertView = rl;
			bih = new BankItemHolder();
			bih.wordView = (TextView) rl.findViewById(R.id.txtWord);
			bih.descriptionView = (TextView) rl.findViewById(R.id.txtDescription);
			convertView.setTag(bih);
		} else {
			bih = (BankItemHolder) convertView.getTag();
		}		

		bih.wordView.setText(items.get(position).getWord());
		bih.descriptionView.setText(items.get(position).getDescriprion());
		
		l=new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String w1 = items.get(position).getWord();
				int i1 = w1.indexOf(" ");
				String w2=w1.substring(0, i1);
				
				Toast.makeText(v.getContext(), "Saying: "+w2, Toast.LENGTH_SHORT).show();
				
				
			
		        
				//b.speakWords(w2);
				//Intent intent= new Intent(v.getContext(), android.activity.TextToSpeach.class);
				//myMainActivity.startActivity(intent);
				//ttl1.speakWords(w2);
			}
		};;;
		
		convertView.setOnClickListener(l);	
		return convertView;
		
	}
	
	/*
	 * se vrsi brisenje na stavkite
	 */
	public void clear() {
		items.clear();
	}

	/*
	 * se vrsi vnesuvanje na stavka
	 */
	public void add(BankItem item) {
		items.add(item);
	}

}
